package com;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.jsbridge.R;

/**
 * @author 关忆北丶
 * @description:
 * @date :2020/7/20 12:53
 */
public class PopupUtil {


    public static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }

    public static PopupWindow showPartyApplyPop(final Activity activity, View contentView, int width, int height) {
        PopupWindow popupWindow = new PopupWindow(contentView, width, height);
        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //点击外部消失
        popupWindow.setOutsideTouchable(false);
        //设置可以点击
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);// 取得焦点
        // 设置这两个属性
        setBackgroundAlpha(activity, 0.5f);//设置屏幕透明度
        //进入退出的动画，指定刚才定义的style
        popupWindow.setAnimationStyle(R.style.camera_popwindow_anim_style);
        popupWindow.setOnDismissListener(() -> setBackgroundAlpha(activity, 1.0f));
        return popupWindow;
    }

    /**
     * 新版粉丝【会员】大礼包弹窗
     *
     * @param activity
     */
    public static PopupWindow showMemberGift(Activity activity, OnViewClickListener listener) {
        View mGroupNotice = LayoutInflater.from(activity).inflate(R.layout.popup_member_gift, null);
        ImageView mOpenFansGift = mGroupNotice.findViewById(R.id.iv_member_opengift);
        PopupWindow popupWindow = PopupUtil.showPartyApplyPop(activity, mGroupNotice, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);// 取得焦点
        setBackgroundAlpha(activity, 0.5f);//设置屏幕透明度
        popupWindow.setAnimationStyle(R.style.camera_popwindow_anim_style);
        popupWindow.setOnDismissListener(() -> setBackgroundAlpha(activity, 1.0f));
        popupWindow.showAtLocation(mGroupNotice, Gravity.CENTER, 0, 0);

        mOpenFansGift.setOnClickListener(v -> {
            popupWindow.dismiss();
            if (listener != null) {
                listener.onClick(mOpenFansGift);
            }
        });
        return popupWindow;
    }




    public interface OnViewClickListener {
        void onClick(View view);
    }

}
